source('code/analysis/load.R')


data = read_dta('data/working/deport-sender.dta') %>%
  mutate(exp = 'deport') %>% 
  bind_rows(read_dta('data/working/defund-sender.dta') %>% mutate(exp = 'defund')) %>% 
  bind_rows(read_dta('data/working/defund-sender-replication.dta') %>% mutate(exp = 'defund-replication') %>% rename(tweet_misleading=misleading)) %>%
  bind_rows(read_dta('data/working/rainforest-sender.dta') %>% mutate(exp = 'rainforest')) %>% 
  bind_rows(read_dta('data/working/daylight-sender.dta') %>% mutate(exp = 'daylight')) %>% 
  bind_rows(read_dta('data/working/defund-hypothetical.dta') %>% mutate(exp = 'defund-hypo')) %>%
  bind_rows(read_dta('data/working/anticipated-persuasion.dta') %>% mutate(exp='anticipated')) %>%
  data_finalize()


coef_sd = function(model) {
  
  coef=formatC(summary(model)$coefficients[,'Estimate']['excuse'], digits=2, format='f')
  sd=formatC(summary(model)$coefficients[,'Std. Error']['excuse'], digits=2, format='f')
  value=paste0(coef,' (',sd,')')
  return(value)
  
}

create_row2 = function(data, condition, label) {
  
  data = data %>% filter(!! rlang::parse_expr(condition))
  
  model1=lm(posted~excuse, data=data %>% filter((exp=='defund-replication'|exp == 'defund') &join==1))
  model2=lm(posted~excuse, data=data %>% filter((exp=='rainforest') &join==1))
  model3=lm(posted~excuse, data=data %>% filter((exp=='daylight') &join==1))
  model4=lm(posted~excuse, data=data %>% filter((exp=='deport') &join==1))
  obs = nobs(model1)

  row=paste0(str_interp('${label} & '), coef_sd(model1), ' & ', coef_sd(model2), ' & ', coef_sd(model3), ' & ', coef_sd(model4), '\\\\')
  
  return(row)
}

a1 = create_row2(data, 'age<25', 'Under 25')
a2 = create_row2(data, 'age>=25 & age<40', '25-40')
a3 = create_row2(data, 'age>=40 & age<60', '40-60')
a4 = create_row2(data, 'age>=60', '60+')

b1 = create_row2(data, 'white==1 & hisp==0', 'White, not Hispanic')
b2 = create_row2(data, 'white==0 | hisp==1', 'Non-white or Hispanic')

c1 = create_row2(data, 'male==1', 'Male')
c2 = create_row2(data, 'male==0', 'Female')

d1 = create_row2(data, 'bachelors==1', "Bachelor's degree or higher")
d2 = create_row2(data, 'bachelors==0', "No bachelor's degree or higher")

out = c(
  '\\begin{table} \\centering',
  '\\caption{Heterogeneity by demographic characteristics (Tweetability experiments)}',
  '\\label{t:sender-demographic-heterogeneity}',
  '\\begin{threeparttable}',
  '\\begin{tabular}{@{\\hspace{5pt}}l@{\\hspace{5pt}}cccc} ',
  '\\toprule', 
  '& \\multicolumn{4}{c}{Treatment effect: coef.~(s.e.) } \\\\ \\cmidrule(rr){2-5}',
  '& Defund & Rainforest & DST & Deport \\\\',
  '\\midrule',
  str_interp('\\multicolumn{5}{l}{\\textbf{Panel A}: Heterogeneity by age} \\\\ \\midrule'),
  a1 %>% paste0('\\quad ', .),
  a2 %>% paste0('\\quad ', .),
  a3 %>% paste0('\\quad ', .),
  a4 %>% paste0('\\quad ', .),
  '\\addlinespace',
  str_interp('\\multicolumn{5}{l}{\\textbf{Panel B}: Heterogeneity by race and ethnicity} \\\\ \\midrule'),
  b1 %>% paste0('\\quad ', .),
  b2 %>% paste0('\\quad ', .),
  '\\addlinespace',
  str_interp('\\multicolumn{5}{l}{\\textbf{Panel C}: Heterogeneity by gender} \\\\ \\midrule'),
  c1 %>% paste0('\\quad ', .),
  c2 %>% paste0('\\quad ', .),
  '\\addlinespace',
  str_interp('\\multicolumn{5}{l}{\\textbf{Panel D}: Heterogeneity by education} \\\\ \\midrule'),
  d1 %>% paste0('\\quad ', .),
  d2 %>% paste0('\\quad ', .),
  '\\bottomrule',
  '\\end{tabular}',
  '\\begin{tablenotes} \\footnotesize',
  paste0('\\item \\textit{Notes:} ',tablenotes[['sender-demographic-heterogeneity']]),
  '\\end{tablenotes} \\end{threeparttable} \\end{table}'
)
write_lines(out, str_interp('output/tables/sender-demographic-heterogeneity.tex'))


